<template>
  <view class="container">
    <chome
      ref="chome"
      :cardList="cardList"
      @reloadServerInfo="reloadServerInfo"
    ></chome>

    <shortcut
      ref="shortcut"
      style="animation-duration: 500ms"
      class="animated fadeIn"
      :infos="iscard"
      :child="idx"
      v-show="isShowMenu"
      @closeMenu="closeMenu"
    ></shortcut>

    <u-modal
      v-model="show"
      :show-cancel-button="true"
      confirm-text="确定"
      :title="value"
      @confirm="confirm"
    >
      <view class="u-update-content">
        <!-- <rich-text :nodes="content"></rich-text> -->
        {{ content }}
      </view>
    </u-modal>
  </view>
</template>

<script>
import chome from "../main/child/chome.vue"; // 首页
import shortcut from "../../components/shortcut/shortcut.vue";
export default {
  components: {
    shortcut,
    chome,
  },
  onPageScroll(e) {
    this.$refs.chome.scroll(e.scrollTop);
  },
  onReady() {
    // this.getUiStyle(); //底部tabbar栏目颜色
  },
  data() {
    return {
      list: [],
      isShowMenu: false,
      cardList: [],
      iscard: "",
      idx: "", //营养包
      show: false,
      value: "",
      content: "",
      id: "",
      // 传递给uni-app"rich-text"组件的内容，可以使用"<br>"进行换行
      cid: "",
    };
  },
  onPullDownRefresh() {
    //监听下拉刷新动作的执行方法，每次手动下拉刷新都会执行一次
    //console.log('refresh');
    setTimeout(function () {
      uni.stopPullDownRefresh(); //停止下拉刷新动画
    }, 1000);
    this.$refs.chome.getNewList();
    this.$refs.chome.initHasIsRead();
    this.getAppointment();
  },
  onReachBottom() {
    console.log("触底了");
    this.$refs.chome.getMoreNewList();
    return;
  },
  methods: {
    //推送Cid
    pushCid() {
      const value = uni.getStorageSync("cid");
      uni.request({
        url: "http://192.168.2.12:8089/test/getCid?cid=" + value,
        success: (res) => {
          console.log(res.data);
        },
      });
    },
    radioGroupChange(v) {
      console.log(v);
    },
    cancel() {
      // uni.setStorageSync("yyState", 0)
      this.config
        .post("/message/getMessageInfo", {
          id: this.id,
        })
        .then((res) => {
          uni.hideLoading();
          if (res.data.code === 200) {
            self.detail = res.data.data;
            this.$refs.chome.initHasIsRead();
            this.getAppointment();
          } else {
            if (res.data.code == "-201") {
              self.config.handleError(res.data);
            } else {
              uni.showToast({
                title: self.config.handleError(res.data),
                icon: "none",
              });
            }
          }
        });
    },
    confirm() {
      let self = this;
      uni.showLoading({
        title: "加载中",
        mask: true,
      });
      setTimeout((s) => {
        uni.hideLoading();
      }, 5000);
      this.config
        .post("/message/getMessageInfo", {
          id: this.id,
        })
        .then((res) => {
          uni.hideLoading();
          if (res.data.code === 200) {
            self.detail = res.data.data;
            this.$refs.chome.initHasIsRead();
            this.getAppointment();
          } else {
            if (res.data.code == "-201") {
              self.config.handleError(res.data);
            } else {
              uni.showToast({
                title: self.config.handleError(res.data),
                icon: "none",
              });
            }
          }
        });
    },
    getAppointment() {
      let self = this;
      this.config.get("/message/getIndexMessageInfo", {}).then((res) => {
        if (res.data.code === 200) {
          // if(res.data.data=='0'){
          // 	uni.navigateTo({url: '/pages/reg/sub-user-info'})
          // }else{
          if (res != null) {
            if (uni.getStorageSync("yyState") == 1) {
              this.value = res.data.data.title;
              this.content = res.data.data.content;
              this.id = res.data.data.messageId;
              this.show = true;
              // this.getPushMessage()
            }
          }
          // }
        } else {
          if (res.data.code == "-201") {
            self.config.handleError(res.data);
          } else {
            uni.showToast({
              title: self.config.handleError(res.data),
              icon: "none",
            });
          }
        }
      });
    },
    getPushMessage() {
      this.config
        .post("/uni/push", {
          cid: this.cid,
          title: this.value,
          content: this.content,
          type: 1,
        })
        .then((res) => {
          console.log(res);
        });
    },
    closeMenu(msg) {
      this.isShowMenu = false;
    },
    // 获取用户是否完善信息
    isCompeletInfo() {
      let self = this;
      this.config.post("/initUser/judge", {}).then((res) => {
        if (res.data.code === 200) {
          // if(res.data.data=='0'){
          // 	uni.navigateTo({url: '/pages/reg/sub-user-info'})
          // }else{
          // self.getCardList()
          self.isPhoneType();
          // }
        } else {
          if (res.data.code == "-201") {
            self.config.handleError(res.data);
          } else {
            uni.showToast({
              title: self.config.handleError(res.data),
              icon: "none",
            });
          }
        }
      });
    },
    isPhoneType() {
      if (uni.getSystemInfoSync().platform === "android") {
        this.getCardLista();
        uni.setStorageSync("device", 0);
      } else {
        this.getCardList();
        uni.setStorageSync("device", 1);
      }
    },
    // 获取首页功能列表
    getCardList() {
      let self = this;
      this.config
        .post("/appIndexMenu/pageList", {
          personType: "3",
        })
        .then((res) => {
          if (res.data.code === 200) {
            self.cardList = res.data.rows;
          } else {
            if (res.data.code == "-201") {
              self.config.handleError(res.data);
            } else {
              uni.showToast({
                title: self.config.handleError(res.data),
                icon: "none",
              });
            }
          }
        });
    },
    sendCid() {
      this.config
        .post("/uni/saveCidToUser", {
          ptType: uni.getStorageSync("device"),
          cid: uni.getStorageSync("cid"),
        })
        .then((res) => {
          // console.log(res)
        });
    },
    //安卓use
    getCardLista() {
      let self = this;
      this.config
        .post("/appIndexMenu/pageListA", {
          personType: "3",
        })
        .then((res) => {
          if (res.data.code === 200) {
            self.cardList = res.data.rows;
          } else {
            if (res.data.code == "-201") {
              self.config.handleError(res.data);
            } else {
              uni.showToast({
                title: self.config.handleError(res.data),
                icon: "none",
              });
            }
          }
        });
    },
    // 获取社区详情
    getUniIntro() {
      let self = this;
      this.config.post("/communityInfo/getCommunityDetail", {}).then((res) => {
        if (res.data.code === 200) {
          this.$store.state.sqname = res.data.data.deptName;
          if (uni.getStorageSync("community")) {
          } else {
            uni.setStorageSync("community", JSON.stringify(res.data.data));
          }
        } else {
          if (res.data.code == "-201") {
            self.config.handleError(res.data);
          } else {
            uni.showToast({
              title: self.config.handleError(res.data),
              icon: "none",
            });
          }
        }
      });
    },
    isReport() {
      let self = this;
      this.config
        .get("/initUser/pdCommonIsArchives", {})
        .then(({ data: { data } }) => {
          console.log(data); //1显示  0不显示
          this.iscard = data;
        });
    },
    getIdx() {
      this.config.get("/flow/getUserNutrition").then((res) => {
        this.idx = res.data.data;
      });
    },
    reloadServerInfo() {
      this.config.getLoginInfo();
      this.getUniIntro();
      this.isShowMenu = false;
      this.isCompeletInfo();
      this.getAppointment();
    },
    // 对于页面中的原生标题栏（TitleNView）以及uni-app中的nvue页面，则需获取当前的外观模式，动态修改样式进行适配。
    getUiStyle() {
      var style = plus.navigator.getUiStyle();
      this.ptcolor(style);
    },
    ptcolor(style) {
      if ("dark" == style) {
        console.log("当前为暗黑模式");
        uni.setTabBarStyle({
          backgroundColor: "#1B1C1E",
        });
        uni.setNavigationBarColor({
          frontColor: "#ffffff",
          backgroundColor: "#1B1C1E",
          animation: {
            duration: 400,
            timingFunc: "easeIn",
          },
        });
        uni.setBackgroundColor({
          backgroundColor: "#1B1C1E", // 窗口的背景色
          backgroundColorTop: "#222222", // 顶部窗口的背景色
          backgroundColorBottom: "#333333", // 底部窗口的背景色
        });
        uni.setBackgroundTextStyle({
          textStyle: "dark",
        });
      } else {
        console.log("当前为普通模式");
        uni.setTabBarStyle({
          backgroundColor: "#fff",
        });
        uni.setBackgroundTextStyle({
          textStyle: "light",
        });
      }
    },
    getUserChange() {
      let self = this;
      this.config.get("/initUser/pdNowUserChange", {}).then((res) => {
        if (res.data.code === 200) {
          if (res.data.data == 1) {
            uni.showToast({
              title:
                "系统检测到你当前身份发生了变化，请你前往“我的-编辑”中完善个人信息",
              icon: "none",
              duration: 3000,
            });
            setInterval((s) => {
              uni.switchTab({
                url: "../user/user",
              });
            }, 3000);
          }
        } else {
          if (res.data.code == "-201") {
            self.config.handleError(res.data);
          } else {
            uni.showToast({
              title: self.config.handleError(res.data),
              icon: "none",
            });
          }
        }
      });
    },
    getOpenCode() {
      let userId = uni.getStorageSync("user").id;
      this.config
        .get("/uni/getIsPush", {
          userId,
        })
        .then((res) => {
          // console.log(res)
          if (res.data.data == 1) {
            uni.setStorageSync("jscid", 1);
            this.sendCid();
          } else {
            uni.setStorageSync("jscid", 2);
          }
        });
    },
  },
  mounted() {
    uni.onTabBarMidButtonTap((s) => {
      this.isShowMenu = !this.isShowMenu;
    });
    this.config.getLoginInfo();
    this.getUniIntro(); //全局信息
    this.$refs.chome.initHasIsRead();
  },
  onThemeChange(res) {
    var style = res.style;
    this.ptcolor(style);
    console.log(style);
  },
  onLoad() {
    // uni.reLaunch({
    // 	url:'../home/home'
    // })
    this.getIdx();
    this.getOpenCode();
    this.getUniIntro();
    this.cid = uni.getStorageSync("cid");
    if (uni.getStorageSync("jscid") == 1) {
      this.sendCid();
    }
    this.$refs.chome.getMoreNewList(); //获取list
  },
  onShow() {
    this.getIdx();
    this.cid = uni.getStorageSync("cid");
    this.getUserChange();
    this.getUniIntro();
    this.getAppointment();
    this.config.getLoginInfo();
    this.isShowMenu = false;
    this.isCompeletInfo();
    setTimeout((s) => {
      this.getOpenCode();
      this.$refs.chome.getFamily();
      this.isReport();
    }, 500);
    if (uni.getStorageSync("wzname") != 1) {
      setTimeout((s) => {
        this.$refs.chome.getNewList();
      }, 300);
    }
    setTimeout((s) => {
      this.$refs.chome.initHasIsRead();
    }, 300);
    uni.setStorageSync("wzname", 0);
    if (uni.getStorageSync("jscid") == 1) {
      self.sendCid();
    }

    let pageList = {};
    const utils = {
      reloadCurrentPage: function (_self, isTab = true) {
        // #ifdef APP-PLUS
        var route = _self.$scope.route;
        var data = _self.$scope.options && _self.$scope.options.data;
        var url = "/" + route;
        if (data) {
          url = "/" + route + "?data=" + data;
        }
        var isRecovery = true;
        let newTime = Date.now();
        if (pageList[url]) {
          const query = uni.createSelectorQuery().in(_self);
          //这里select()中替换为自己的样式class名称
          query
            .select(".container")
            .fields({ size: true }, (data) => {
              isRecovery = false;
            })
            .exec();
          setTimeout(() => {
            if (isRecovery) {
              //如果获取不到节点
              //确保只刷新一次
              if (newTime - pageList[url] > 3000) {
                //超过3秒才重新刷新，这里设置几秒就行，目的是防止无限刷新
                //因为刷新后页面肯定会出来，但是立马再次调用该方法不一定能获取节点
                pageList[url] = newTime;
                if (isTab) {
                  uni.reLaunch({
                    url,
                  });
                } else {
                  uni.redirectTo({
                    url,
                  });
                }
              }
            }
          }, 600);
        } else {
          pageList[url] = newTime;
        }
        // if (plus.os.name === 'iOS') {

        // }
        // #endif
      },
    };
  },
};
</script>


<style lang="scss" scoped>
.u-update-content {
  font-size: 26rpx;
  color: $u-content-color;
  line-height: 1.7;
  padding: 30rpx;
}

@media (prefers-color-scheme: closedark) {
  .u-update-content {
    font-size: 26rpx;
    color: #ebeeed;
    line-height: 1.7;
    padding: 30rpx;
  }
}
</style>
